package com.joolun.web.controller.scrm;

import java.util.List;

import com.joolun.common.core.domain.entity.SysDept;
import com.joolun.common.utils.StringUtils;
import com.joolun.weixin.service.IScrmDepartmentService;
import jakarta.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.system.domain.ScrmDepartment;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 企业部门Controller
 * 
 * @author joolun
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/scrm/department")
public class ScrmDepartmentController extends BaseController
{
    @Autowired
    private IScrmDepartmentService scrmDepartmentService;


    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public AjaxResult list(ScrmDepartment scrmDepartment)
    {
        List<ScrmDepartment> depts = scrmDepartmentService.selectScrmDepartmentList(scrmDepartment);
        return success(depts);
    }


    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
    {
        List<ScrmDepartment> depts = scrmDepartmentService.selectScrmDepartmentList(new ScrmDepartment());
        depts.removeIf(d -> d.getId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return success(depts);
    }


    /**
     * 导出企业部门列表
     */
    @PreAuthorize("@ss.hasPermi('scrm:department:export')")
    @Log(title = "企业部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrmDepartment scrmDepartment)
    {
        List<ScrmDepartment> list = scrmDepartmentService.selectScrmDepartmentList(scrmDepartment);
        ExcelUtil<ScrmDepartment> util = new ExcelUtil<ScrmDepartment>(ScrmDepartment.class);
        util.exportExcel(response, list, "企业部门数据");
    }

    /**
     * 获取企业部门详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scrmDepartmentService.selectScrmDepartmentById(id));
    }

    /**
     * 新增企业部门
     */
    @PreAuthorize("@ss.hasPermi('scrm:department:add')")
    @Log(title = "企业部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScrmDepartment scrmDepartment)
    {
        scrmDepartment.setCreateBy(getUsername());
        return toAjax(scrmDepartmentService.insertScrmDepartment(scrmDepartment));
    }

    /**
     * 修改企业部门
     */
    @PreAuthorize("@ss.hasPermi('scrm:department:edit')")
    @Log(title = "企业部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScrmDepartment scrmDepartment)
    {
        scrmDepartment.setCreateBy(getUsername());
        return toAjax(scrmDepartmentService.updateScrmDepartment(scrmDepartment));
    }

    /**
     * 删除企业部门
     */
    @PreAuthorize("@ss.hasPermi('scrm:department:remove')")
    @Log(title = "企业部门", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scrmDepartmentService.deleteScrmDepartmentByIds(ids));
    }

    /**
     * 同步企业部门
     */
    @GetMapping("/sync")
    public AjaxResult sync(String extCorpId ) throws WxErrorException {
        scrmDepartmentService.sync(extCorpId);
        return AjaxResult.success();
    }

}
